/**
* Copyright (c) 2015-2016 VisionStar Information Technology (Shanghai) Co., Ltd. All Rights Reserved.
* EasyAR is the registered trademark or trademark of VisionStar Information Technology (Shanghai) Co., Ltd in China
* and other countries for the augmented reality technology developed by VisionStar Information Technology (Shanghai) Co., Ltd.
*/

package data.easykey.leolu.nativevideodemo;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import cn.easyar.engine.EasyAR;
import data.easykey.leolu.nativevideodemo.jnin.JNICallBack;

/**
 * 识别界面， PS： preview 的父控件必须是 RelativeLayout，否则无法识别
 */
public class MainActivity extends Activity implements View.OnClickListener{

    /*
    * Steps to create the key for this sample:
    *  1. login www.easyar.com
    *  2. create app with
    *      Name: HelloARVideo
    *      Package Name: cn.easyar.samples.helloarvideo
    *  3. find the created item in the list and show key
    *  4. set key string bellow
    */
    static String key = "5k55nPdXmQwIaxup5ePmfo4deFHAuPDCZOdCKnCcAJxi48d8zxSRf8bOlR9tJst6Yfp576NNxoE3OfqbQ17XT64FgBcqV6Wvnk7n9a2a1b5f6daefccf35171c47b83b9a38otjJrpypjfKXOdhJhU3oHuKLOcNdPPw6cO01OWy3BkGiW2lYugvFZyjDyJRAGKr5ZWq4";

    static {
        System.loadLibrary("native");
    }

    public static native void nativeInitGL();
    public static native void nativeResizeGL(int w, int h);
    public static native void nativeRender();
    private native boolean nativeInit();
    private native void nativeDestory();
    private native void nativeRotationChange(boolean portrait);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        initView();
//        startAR();

//        EasyAR.initialize(this, key);
//        nativeInit();
//
//        GLView glView = new GLView(this);
//        glView.setRenderer(new Renderer());
//        glView.setZOrderMediaOverlay(true);
//
//        ((ViewGroup) findViewById(R.id.preview)).addView(glView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//        nativeRotationChange(getWindowManager().getDefaultDisplay().getRotation() == android.view.Surface.ROTATION_0);
//
//        JNICallBack back = new JNICallBack();
//        Log.i("thduan", "开始 back.nativeStart()");
//        back.nativeStart();
    }

    private void initView() {
        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);
    }

    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
        nativeRotationChange(getWindowManager().getDefaultDisplay().getRotation() == android.view.Surface.ROTATION_0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        nativeDestory();
    }
    @Override
    protected void onResume() {
        super.onResume();
        EasyAR.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        EasyAR.onPause();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start:
                startAR();
                break;
            case R.id.stop:
                stopAR();
                break;
            default:
                break;
        }
    }

    private void startAR() {
        EasyAR.initialize(this, key);
        nativeInit();

        //初始化视频显示view
        GLView glView = new GLView(this);
        glView.setRenderer(new Renderer());
        glView.setZOrderMediaOverlay(true);

        ((ViewGroup) findViewById(R.id.preview)).addView(glView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        //可以不支持横竖屏切换
        nativeRotationChange(getWindowManager().getDefaultDisplay().getRotation() == android.view.Surface.ROTATION_0);


        JNICallBack back = new JNICallBack();
        Log.i("thduan", "开始 back.nativeStart()");
        back.nativeStart();
    }

    private void stopAR() {
        nativeDestory();
    }
}
